package com.top.art.tool;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.util.Assert;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * The type Ali yun code.
 *
 * @author hjh
 * @version 2020 /10/7-13:46
 */
@Slf4j
public class AliYunCode {


    /**
     * Do identify.
     *
     * @param url the url
     * @return the string
     */
    public static String decode(String url) {
        Assert.hasLength(url, "url is not null");
        //API产品路径
        String requestUrl = "https://iqrcode.market.alicloudapi.com/ai_market/ai_ocr_universal/qrcode/v1";
        //阿里云APPCODE
        String appcode = "eee99e5613e748bcaeb6a4bdd72c279d";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        //根据API的要求，定义相对应的Content-Type
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("IMAGE", url.trim());
        bodys.put("IMAGE_TYPE", "1");
        String response = null;
        try {
            response = post(requestUrl, headers, bodys);
            log.info(response);
        } catch (Exception e) {
            log.error("阿里云二维码识别失败", e);
        }
        if (response != null) {
            JSONObject object = JSON.parseObject(response);
            JSONArray array = object.getJSONArray("QRCODE_ENTITY");
            if (array.size() == 1) {
                JSONObject object1 = array.getJSONObject(0);
                JSONArray array1 = object1.getJSONArray("QRCODE_INFO");
                if (array1.size() == 1) {
                    return array1.getString(0);
                }
            }
        }
        return null;
    }

    /**
     * Post string.
     *
     * @param url the url
     * @param headers the headers
     * @param body the body
     * @return the string
     * @throws IOException the io exception
     */
    public static String post(String url, Map<String, String> headers, Map<String, String> body) throws IOException {
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(url);
        // 必须设置下面这个Header
        for (String key : headers.keySet()) {
            postMethod.addRequestHeader(key, headers.get(key));
        }

        List<NameValuePair> bodyPair = new ArrayList<>();
        for (String key : body.keySet()) {
            bodyPair.add(new NameValuePair(key, body.get(key)));
        }
        NameValuePair[] bodyKvs = new NameValuePair[body.size()];
        postMethod.setRequestBody(bodyPair.toArray(bodyKvs));

        int code = client.executeMethod(postMethod);
        if (code == 200) {
            return postMethod.getResponseBodyAsString();
        } else {
            System.out.println(code);
        }
        throw new IOException("请求失败");
    }

    public static void main(String[] args) {
        System.out.println(decode("https://topexam.oss-cn-hangzhou.aliyuncs.com/paper/sumiao/20201007203309.jpg"));
    }

}
